﻿using HalconDotNet;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.Tools;
using Halcon二次开发.View.距离转换工具;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.Controller.距离转换工具
{
    public class DistanceToolController
    {
        /// <summary>
        /// 距离转换界面
        /// </summary>
        DistanceToolForm distanceToolForm;

        /// <summary>
        /// 控件句柄
        /// </summary>
        HWindow hWindon;
       public double Sum3 = 0;

        #region 输入输出参数
        public double[] HideIntputArrayDistances;

        public double[] HideOutputArrayDistances;
        #endregion

        public DistanceToolController(DistanceToolForm distanceToolForm,HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            //获取当前工位
            hWindon = hwindowFlowNode.hSmartWindowControl1.HalconWindow;
            this.distanceToolForm = distanceToolForm;
        }

        public DistanceToolController()
        {
        }

        bool paper = true;
        /// <summary>
        /// 运行
        /// </summary>
        public void Run()
        {
            if (paper)
            {
                Suan();
                paper = false;
            }
            distanceToolForm.textBox4.Text = "";
            try
            {
                if (HideIntputArrayDistances==null) { return; }
                string IntputDistance = null;
                foreach (double item in HideIntputArrayDistances)
                {
                    IntputDistance += "[" + item + "]";
                }
                distanceToolForm.textBox5.Text = IntputDistance;


                distanceToolForm.textBox4.Text = Sum3.ToString();

                string bilii = null;
                foreach (double item in HideIntputArrayDistances)
                {
                    bilii += "[" + item * Sum3 + "]";
                }
                distanceToolForm.textBox3.Text = bilii;

                HideOutputArrayDistances = new double[HideIntputArrayDistances.Length];

                for (int i = 0; i < HideOutputArrayDistances.Length; i++)
                {
                    HideOutputArrayDistances[i] = HideIntputArrayDistances[i]*Sum3;
                }
            }
            catch (Exception ex)
            {
                HideOutputArrayDistances = null;
                //MessageBox.Show("运算错误：" + ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                throw new Exception("运算错误：" + ex.Message);
            }
        }
        public void HardRun(DistanceToolForm distanceToolForm,   double[] HideIntputArrayDistances,ref double[] HideOutputArrayDistances)
        {
            double Sum1 = double.Parse(distanceToolForm.textBox1.Text.Trim());
            double Sum2 = double.Parse(distanceToolForm.textBox2.Text.Trim());
            double Sum3 = Sum2 / Sum1;
            try
            {
                if (HideIntputArrayDistances == null) { return; }
                string IntputDistance = null;
                foreach (double item in HideIntputArrayDistances)
                {
                    IntputDistance += "[" + item + "]";
                }
               

                string bilii = null;
                foreach (double item in HideIntputArrayDistances)
                {
                    bilii += "[" + item * Sum3 + "]";
                }

                HideOutputArrayDistances = new double[HideIntputArrayDistances.Length];

                for (int i = 0; i < HideOutputArrayDistances.Length; i++)
                {
                    HideOutputArrayDistances[i] = HideIntputArrayDistances[i] * Sum3;
                }
            }
            catch (Exception ex)
            {
                HideOutputArrayDistances = null;
                //MessageBox.Show("运算错误：" + ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                throw new Exception("运算错误：" + ex.Message);
            }
        }
        public void Suan()
        {
            double Sum1 =double.Parse( distanceToolForm.textBox1.Text.Trim());
            double Sum2 = double.Parse(distanceToolForm.textBox2.Text.Trim());
             Sum3 = Sum2 / Sum1;
            distanceToolForm.textBox4.Text = Sum3.ToString();
        }
    }
    
}
